import { getCookie, deleteCookie } from "../js/cookie.js";
import { addToShoppingCar, searchGoodsByGoodsId } from "../js/interface.js"

//标识用户身份
// var cookie = document.cookie;
// if (cookie) {
//     var lgc = getCookie("lgc");
//     if (lgc) {
//         $(".hide a:first-child").css("display", "none");
//         $(".hide a:last-child").css("display", "block");
//         $(".hide").eq(0).children().html(`欢迎,${lgc}`)
//         // $(".hide a:last-child").html(`欢迎,${lgc}`);
//         $(".exit").click(function () {
//             deleteCookie("lgc");
//             location.reload();
//         })
//     }
// }


$(async function(){
    //标识用户身份
    var user = getCookie("lgc");
    if(user){
        $(".hide a:first-child").css("display", "none");
        $(".hide a:last-child").css("display", "block");
        $(".hide").eq(0).children().html(`欢迎,${user}`);
        $(".exit").click(function () {
            deleteCookie("lgc");
            location.reload();
        })
    }
    // else{
    //     location.href = "../html/login.html?returnUrl=" + encodeURIComponent(location.href);
    // }

    var search = location.search;
    if(search){
        var gid = search.substring(1);
    }
    // console.log(str);
    if(gid){
        var result = await searchGoodsByGoodsId({ gid });
        // console.log(result);
        var { status, msg, data } = result;
        if(status){
            var { goodsName, goodsPrice, smallPicList, bigPicList } = data;

            var picmainHtml = "";
            smallPicList.forEach((img) => {
                picmainHtml += `<li><a><img src="https:${img}" alt=""></a></li>`
            })
            $(".icmain").children().html(picmainHtml);
            $(".icmain").children().children().eq(0).addClass("active");

            var pictbigHtml = "";
            bigPicList.forEach((img) => {
                pictbigHtml += `<li><img src="https:${img}" alt=""></li>`
            })
            $(".picmain").children().eq(0).html(pictbigHtml);
            $(".picmain").children().children().eq(0).addClass("onshow");

            var lookHtml = "";
            bigPicList.forEach((img) => {
                lookHtml += `<img src="https:${img}" alt="">`
            })
            $(".fang").html(lookHtml);
            $(".fang").children().eq(0).addClass("look");
            // $(".fang").children().html(lookHtml);
            // $(".fang").children().children().eq(0).addClass("look");
            $(".fang").css("display","none");

            $(".meta").html(`<h1>${goodsName}</h1>`);

            // var gpHtml = goodsPrice.toFixed(2);
            $(".gp").html((goodsPrice*1).toFixed(2));

            //图片点击切换
            $(".icmain li").on("mouseenter",function(){
                $(this).addClass("active").siblings().removeClass("active");
                var index = $(this).index();
                $(".picmain li").eq(index).addClass("onshow").siblings().removeClass("onshow");
                $(".fang img").eq(index).addClass("look").siblings().removeClass("look");
                // $(".fang").children().eq(index).addClass("look").siblings().removeClass("look");
            })

            //件数加减
            var num = 1;
            $(".add").click(function(){
                num++;
                $(".count").children().val(num);
            })
            $(".reduce").click(function(){
                if(num <= 1)return false;
                num--;
                $(".count").children().val(num);
            })
        }
    }

})


//放大镜
var scale = null;
var maxLeft = null;
var maxTop = null;

$(".picmain").mouseenter(function(){
    $(".fang").css("display","block");
    $(".shadow").css("display","block");

    maxLeft = $(".picmain").width()-$(".shadow").width();
    maxTop = $(".picmain").height()-$(".shadow").height();
    scale = 2;
}).mouseleave(function(){
    $(".fang").css("display","none");
    $(".shadow").css("display","none");
});

$(".picmain").mousemove(function(event){
    var event = event || window.event;
    var x = event.pageX - $(".tool").offset().left - $(".shadow").width()/2;
    var y = event.pageY - $(".tool").offset().top - $(".shadow").height()/2;

    if(x <= 0) x = 0;
    if(x >= maxLeft) x  = maxLeft;
    if(y <= 0) y = 0;
    if(y >= maxTop) y = maxTop;
    var shal = x +"px";
    var shat = y +"px";

    $(".shadow").css("left",shal);
    $(".shadow").css("top",shat);

    var fanff = - x*scale + "px";
    var fanft = - y*scale + "px";
    $(".fang").find(".look").css("left",fanff);
    $(".fang").find(".look").css("top",fanft);
    // console.log($(".fang").children());

})


//加入购物车
$(".jion").on("click",async function(){
    var search = location.search;
    if(search){
        var gid = search.substring(1);
    }
    var user = getCookie("lgc");
    if(user){
        var num = $(".count").children().val();
        var result = await addToShoppingCar({ user, gid ,num });
        var { status, msg } = result;
        if(status){
            location.href = "../html/shoppingcar.html";
        }else{
            alert(msg);
        }
    }else{
        location.href = "../html/login.html?returnUrl=" + encodeURIComponent(location.href);
    }
})



$(".ple").click(function(){
    location.href = "../html/login.html?returnUrl=" + encodeURIComponent(location.href)
})

// function urlParse() {
//     var search = location.search;
//     var data = {};
//     if (search) {  //从注册页跳转过来  => 有数据
//         var str = search.substring(1);
//         console.log(str);
//         var list = str.split("&");
//         // console.log(list);
//         list.forEach(function (item) {
//             var key = item.split("=")[0];
//             var val = item.split("=")[1];
//             data[key] = val;
//         })
//     }
//     return data;
// }